package com.itheima.reggie.controller;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.reggie.common.R;
import com.itheima.reggie.entity.Category;
import com.itheima.reggie.service.ICategoryService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 菜品及套餐分类 前端控制器
 * </p>
 *
 * @author 
 * @since 2024-05-22
 */
@Slf4j
@RestController
@RequestMapping("/category")
public class CategoryController {

    @Autowired
    public ICategoryService categoryService;

    /**
     * 添加分类功能接口
     * @param category
     * @return
     */
    @PostMapping
    public R<String> addCategory(@RequestBody Category category){
        categoryService.save(category);
        return R.success("分类添加成功!");
    }

    /**
     * 分类分页查询功能接口
     * @param page
     * @param pageSize
     * @return
     */
    @GetMapping("/page")
    public R<Page> pageQuery(Integer page,Integer pageSize){
        log.info("第几页:{},每页记录数:{}",page,pageSize);
        return categoryService.pageQuery(page,pageSize);
    }

    /**
     * 根据id删除分类功能接口
     * @param id
     * @return
     */
    @DeleteMapping
    public R<String> deleteCategory(@RequestParam(name = "ids") Long id){
        categoryService.remove(id);
        return R.success("删除成功!");
    }

    /**
     * 修改分类功能接口
     * @param category
     * @return
     */
    @PutMapping
    public R<String> updateCategory(@RequestBody Category category){
        categoryService.saveOrUpdate(category);
        return R.success("分类修改成功");
    }

    @GetMapping("/list")
    public R<List<Category>> getCategoryByType(Category category){
        return categoryService.getCategoryByType(category);
    }

}
